package com.sskj.mine.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.sskj.common.util.GSonUtil;
import com.sskj.mine.bean.AddressList;
import com.sskj.mine.bean.WalletAddressBean;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.ui.activity.WalletAddressManagerActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class WalletAddressManagerActivityPresenter extends BasePresenter<WalletAddressManagerActivity> {


    /**
     * 获取钱包列表
     *
     * @return
     */
    public void getWalletList() {
        httpService.getWalletList()
                .execute(new JsonCallBack<Map>() {
                    @Override
                    public void onSuccess(Response<Map> response) {
                        Map map = response.body();
                        ArrayList<WalletAddressBean> walletAddressBeans = new ArrayList<>();
                        if ((Double) map.get("code") == HttpConfig.OK) {
                            Map<String, Object> coin = (Map) map.get("data");
                            Iterator<Map.Entry<String, Object>> iterator = coin.entrySet().iterator();
                            while (iterator.hasNext()) {
                                Map.Entry<String, Object> next = iterator.next();
                                String key = next.getKey();
                                WalletAddressBean walletAddressBean = new WalletAddressBean(key, key);
                                Object value = next.getValue();
                                String s = GSonUtil.GsonString(value);
                                Gson gson = new Gson();
                                List<AddressList.EthBean> list = gson.fromJson(s, new TypeToken<List<AddressList.EthBean>>() {
                                }.getType());
                                walletAddressBean.setList(list);
                                walletAddressBeans.add(walletAddressBean);
                            }
                        }
                        mView.updateUI(walletAddressBeans);
                    }

                    @Override
                    public void onError(Response<Map> response) {
                        super.onError(response);
                    }
                });
    }

    /**
     * 删除钱包地址
     *
     * @param id 钱包地址Id
     * @return
     */
    public void delWalletAddress(String id) {
        httpService.delWalletAddress(id)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            getWalletList();
                        }
                    }
                });
    }
}
